package tum.in.rost.pp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class InstanceDataParserOld {

	public static final String SINGLE_INSTANCE_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\1st Instance.txt";

	public static final String INSTANCE_FILE_PATH = "E:\\22instances.arff";

	public static final String DATASET_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\tmps.arff";

	public static final String TRAIN_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\train.arff";

	public static final String TEST_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\test.arff";

	public static final String CROSS_TRAIN_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\crossTrain.arff";

	public static final String ATRIBUTE_HEADER_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\headers.txt";

	public static final String ATRIBUTE_HEADER = "@ATTRIBUTE ";

	public static final String HEADER_FILE_NAME_LINE = "@RELATION 'tmps.arff'";

	public static Hashtable<String, Integer> attributeValueHashTable = new Hashtable<>();

	public static final List<String> attributeList = new ArrayList<String>();

	public static final List<String> attributesWithCompleteList = Arrays.asList(new String[] { "pssm", "perc", "psic" });

	public static final String aminoAcidList[] = { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V" };

	public static final int aminoAcidCount = aminoAcidList.length;

	public static final String WEKA_INPUT_FILE_PATH = "C:\\Users\\Ajay\\Desktop\\WekaInput.arff";

	public static final String INPUT_HEADER_FILE_NAME_LINE = "@RELATION 'WekaInput.arff'";

	public static Collection<String> specifiedAttrributes;

	private static int windowSize;

	public static final void parseAttributeFile(String filename) throws IOException {
		int i = 0;
		BufferedReader bis = new BufferedReader(new FileReader(new File(filename)));
		String newLine;
		while ((newLine = bis.readLine()) != null) {
			if (StringUtils.startsWith(newLine, ATRIBUTE_HEADER)) {
				newLine = StringUtils.substringBetween(newLine, ATRIBUTE_HEADER, " ");
				attributeValueHashTable.put(newLine, i);
				i++;
			}
		}
		bis.close();
	}

	public static void parseDataSet(String features[]) throws IOException {
		for (String key : specifiedAttrributes) {
			List<String> indexArray;
			if (attributesWithCompleteList.contains(key)) {
				indexArray = getindexesForWindowSizeTotalSet();
			} else {
				indexArray = getindexesForWindowSizeSingleSet();
			}
			for (int i = 0; i < indexArray.size(); i++) {
				System.out.println("index is" + "-->" + indexArray.get(i) + key);
				int coloumnKey = attributeValueHashTable.get(indexArray.get(i) + key);
				System.out.println(" and value is  " + features[0] + "," + features[coloumnKey]);
			}
		}
	}

	public static List<String> getindexesForWindowSizeTotalSet() {
		List<String> indexes = new ArrayList<>();
		if (windowSize % 2 == 1 && windowSize > 0 && windowSize < 25) {
			int rangeMin = -(windowSize / 2);
			while (indexes.size() != windowSize * aminoAcidCount) {
				for (int k = 0; k < aminoAcidCount && indexes.size() < windowSize * aminoAcidCount; k++) {
					indexes.add(rangeMin + "_" + aminoAcidList[k] + "_");
				}
				rangeMin++;
			}
			return indexes;
		} else {
			throw new IllegalStateException();
		}
	}

	public static List<String> getindexesForWindowSizeSingleSet() {
		List<String> indexes = new ArrayList<>();
		if (windowSize % 2 == 1 && windowSize > 0 && windowSize < 25) {
			int rangeMin = -(windowSize / 2);
			for (int i = rangeMin; i <= -(rangeMin); i++) {
				indexes.add(i + "_");
			}
			return indexes;
		} else {
			throw new IllegalStateException();
		}
	}

	public static void createNewTestArffFile() throws IOException {
		BufferedReader bis = new BufferedReader(new FileReader(new File(TEST_FILE_PATH)));
		BufferedWriter bisWriteTrain = new BufferedWriter(new FileWriter(new File(WEKA_INPUT_FILE_PATH)));

		bisWriteTrain.write(INPUT_HEADER_FILE_NAME_LINE);
		createHeadersForArffFile(bisWriteTrain);
		createInstanceDataForArffFile(bisWriteTrain);
		bisWriteTrain.close();
	}

	public static void createInstanceDataForArffFile(BufferedWriter bisWriteTrain) {

	}

	public static void createHeadersForArffFile(BufferedWriter bisWriteTrain) {

	}

	public static void parseInstanceFile(String fileName) throws IOException {
		BufferedReader bis = new BufferedReader(new FileReader(new File(fileName)));
		String newLine;
		while ((newLine = bis.readLine()) != null) {
			String features[] = newLine.split(",");
			if (features.length == 2886) {
				parseDataSet(features);
			} else {
				throw new IllegalStateException();
			}
		}
	}

	public static void init() throws IOException {
		// DataSplit split = new DataSplit();
		// split.splitdata(DATASET_FILE_PATH);
		parseAttributeFile(ATRIBUTE_HEADER_FILE_PATH);
	}

	public static void main(String args[]) throws IOException {
		specifiedAttrributes = new ArrayList<>(Arrays.asList(args));
		windowSize = Integer.parseInt(args[0]);
		specifiedAttrributes.remove(args[0]);
		Long l = System.currentTimeMillis();
		init();
		createNewTestArffFile();
		// parseInstanceFile(TEST_FILE_PATH);
		System.out.println(System.currentTimeMillis() - l);
	}

}
